﻿namespace Yom.WebService.Admin
{
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Web;
    using Yom.Common;

    public class AdminComic : AdminBase
    {
        public void OutWrite()
        {
            string ID = Factory.Request("ID");
            string CategoryID = Factory.Request("CategoryID");
            string Author = Factory.Request("Author");
            string FacePath = Factory.Request("Face");
            string ComicName = Factory.Request("ComicName");
            string OtherName = Factory.Request("OtherName");
            string State = Factory.Request("State");
            string Tag = Factory.Request("Tag");
            string Intro = Factory.Request("Intro");
            string Language = Factory.Request("Language");
            string Color = Factory.Request("Color");
            string IsTop = Factory.Request("IsTop");
            string IsCheck = Factory.Request("IsCheck");
            string Action = Factory.Request("Action");
            StringBuilder builder;

            switch (Action)
            {
                case "Add":
                    if (string.IsNullOrEmpty(ComicName))
                    {
                        builder = new StringBuilder();
                        builder.Append("<script type=\"text/javascript\" src=\"Images/Js/jquery.uploadify.js\"></script>");
                        builder.Append("<fieldset class=\"note\"><dl><dd>漫画标题</dd>");
                        builder.Append("<dt><input id=\"comicname\" type=\"text\" class=\"txt\" /></dt></dl>");
                        builder.Append("<dl><dd>所属分类</dd><dt><select id=\"categoryid\">");
                        List<Yom.Model.Category> calist = new Yom.Data.Category().GetList();
                        foreach (Yom.Model.Category cam in calist)
                        {
                            builder.Append("<option value=\"" + cam.CategoryID.ToString() + "\">" + cam.CategoryName + "</option>");
                        }
                        builder.Append("</select></dt></dl>");
                        builder.Append("<dl><dd>漫画作者</dd><dt><input type=\"text\" class=\"txt\" id=\"author\" /></dt></dl>");
                        builder.Append("<dl><dd>其他名称</dd><dt><input type=\"text\" class=\"txt\" id=\"othername\" /></dt></dl>");
                        builder.Append("<dl><dd>漫画封面</dd><dt><input name=\"face\" id=\"face\" type=\"file\" /><span id=\"progress\"></span></dt></dl>");
                        builder.Append("<dl><dd>漫画状态</dd><dt><select id=\"state\"><option value=\"0\">连载</option><option value=\"1\">暂停</option><option value=\"2\">完结</option></select></dt></dl>");
                        builder.Append("<dl><dd>漫画标签</dd><dt><input type=\"text\" class=\"txt\" id=\"tag\" /></dt></dl>");
                        builder.Append("<dl><dd>漫画语言</dd><dt><select id=\"language\"><option value=\"0\">简体</option><option value=\"1\">繁体</option><option value=\"2\">日语</option><option value=\"3\">英语</option><option value=\"4\">其他</option></select></dt></dl>");
                        builder.Append("<dl><dd>漫画颜色</dd><dt><select id=\"color\"><option value=\"0\">黑白</option><option value=\"1\">彩色</option><option value=\"2\">未知</option></select></dt></dl>");
                        builder.Append("<dl><dd>漫画简介</dd><dt><textarea cols=\"50\" rows=\"5\" id=\"intro\"></textarea></dt></dl>");
                        builder.Append("</fieldset><div id=\"ErrShow\" style=\"color:red\"></div><div class=\"SubButton\"><input type=\"button\" id=\"Submit\" value=\"添加漫画\" class=\"button\" /></div>");
                        base.Response.Write(builder.ToString());
                    }
                    else
                    {
                        Yom.Model.Comic model = new Yom.Model.Comic();
                        model.CategoryID = CategoryID.ToInt();
                        model.Author = Author;
                        model.Color = Color.ToInt();
                        model.ComicName = ComicName;
                        model.Initial = System.Convert.ToChar(Factory.GetCharSpellCode(ComicName));
                        string ext = System.IO.Path.GetExtension(FacePath);
                        model.FaceExt = ext.Substring(1, ext.Length - 1);
                        model.Intro = Intro;
                        model.IsCheck = false;
                        model.JoinTime = System.DateTime.Now;
                        model.Language = Language.ToInt();
                        model.LastTime = System.DateTime.Now;
                        model.OtherName = OtherName;
                        model.State = State.ToInt();
                        model.Tag = Tag;
                        long ComicID = new Yom.Data.Comic().Add(model);
                        Fso.MoveFile(FacePath, SiteLoad.Scheme.SaveFace + "/" + System.Math.Ceiling(System.Convert.ToDouble(ComicID) / 1000).ToString() + "/" + ComicID.ToString() + "." + model.FaceExt);
                        if (ComicID > 0)
                            base.Response.Write("1");
                        else
                            base.Response.Write("0");
                    }
                    break;
                case "Edit":
                    if (!string.IsNullOrEmpty(ID))
                    {
                        if (string.IsNullOrEmpty(ComicName))
                        {
                            Yom.Model.Comic model = new Yom.Data.Comic().GetModel(ID.ToLong());
                            builder = new StringBuilder();
                            builder.Append("<script type=\"text/javascript\" src=\"Images/Js/jquery.uploadify.js\"></script>");
                            builder.Append("<fieldset class=\"note\"><dl><dd>漫画标题</dd>");
                            builder.Append("<dt><input id=\"comicname\" type=\"text\" class=\"txt\" value=\"" + model.ComicName + "\" /></dt></dl>");
                            builder.Append("<dl><dd>所属分类</dd><dt><select id=\"categoryid\">");
                            List<Yom.Model.Category> calist = new Yom.Data.Category().GetList();
                            foreach (Yom.Model.Category cam in calist)
                            {
                                if (cam.CategoryID == model.CategoryID)
                                    builder.Append("<option value=\"" + cam.CategoryID.ToString() + "\" selected=\"selected\">" + cam.CategoryName + "</option>");
                                else
                                    builder.Append("<option value=\"" + cam.CategoryID.ToString() + "\">" + cam.CategoryName + "</option>");
                            }
                            builder.Append("</select></dt></dl>");
                            builder.Append("<dl><dd>漫画作者</dd><dt><input type=\"text\" class=\"txt\" id=\"author\" value=\"" + model.Author + "\" /></dt></dl>");
                            builder.Append("<dl><dd>其他名称</dd><dt><input type=\"text\" class=\"txt\" id=\"othername\" value=\"" + model.OtherName + "\" /></dt></dl>");
                            builder.Append("<dl><dd>漫画封面</dd><dt><input name=\"face\" id=\"face\" type=\"file\" /><span id=\"progress\"></span></dt></dl>");
                            builder.Append("<dl><dd>漫画状态</dd><dt><select id=\"state\"><option value=\"0\"" + ((model.State == 0) ? " selected=\"selected\"" : "") + ">连载</option><option value=\"1\"" + ((model.State == 1) ? " selected=\"selected\"" : "") + ">暂停</option><option value=\"2\"" + ((model.State == 2) ? " selected=\"selected\"" : "") + ">完结</option></select></dt></dl>");
                            builder.Append("<dl><dd>漫画标签</dd><dt><input type=\"text\" class=\"txt\" id=\"tag\" value=\"" + model.Tag + "\" /></dt></dl>");
                            builder.Append("<dl><dd>漫画语言</dd><dt><select id=\"language\"><option value=\"0\"" + ((model.Language == 0) ? " selected=\"selected\"" : "") + ">简体</option><option value=\"1\"" + ((model.Language == 1) ? " selected=\"selected\"" : "") + ">繁体</option><option value=\"2\"" + ((model.Language == 2) ? " selected=\"selected\"" : "") + ">日语</option><option value=\"3\"" + ((model.Language == 3) ? " selected=\"selected\"" : "") + ">英语</option><option value=\"4\"" + ((model.Language == 4) ? " selected=\"selected\"" : "") + ">其他</option></select></dt></dl>");
                            builder.Append("<dl><dd>漫画颜色</dd><dt><select id=\"color\"><option value=\"0\"" + ((model.Color == 0) ? " selected=\"selected\"" : "") + ">黑白</option><option value=\"1\"" + ((model.Color == 1) ? " selected=\"selected\"" : "") + ">彩色</option><option value=\"2\"" + ((model.Color == 2) ? " selected=\"selected\"" : "") + ">未知</option></select></dt></dl>");
                            builder.Append("<dl><dd>漫画简介</dd><dt><textarea cols=\"50\" rows=\"5\" id=\"intro\">" + model.Intro + "</textarea></dt></dl>");
                            builder.Append("</fieldset><div id=\"ErrShow\" style=\"color:red\"></div><div class=\"SubButton\"><input type=\"button\" id=\"Submit\" value=\"修改漫画\" class=\"button\" /></div>");
                            base.Response.Write(builder.ToString());
                        }
                        else
                        {
                            Yom.Model.Comic model = new Yom.Data.Comic().GetModel(ID.ToLong());
                            model.Author = Author;
                            model.CategoryID = CategoryID.ToInt();
                            model.Color = Color.ToInt();
                            model.ComicName = ComicName;
                            if (!string.IsNullOrEmpty(FacePath) && FacePath != "undefined")
                            {
                                Fso.DeleteFile(SiteLoad.Scheme.SaveFace + "/" + System.Math.Ceiling(System.Convert.ToDouble(model.ComicID) / 1000).ToString() + "/" + model.ComicID.ToString() + "." + model.FaceExt);
                                string ext = System.IO.Path.GetExtension(FacePath);
                                model.FaceExt = ext.Substring(1, ext.Length - 1);
                                Fso.MoveFile(FacePath, SiteLoad.Scheme.SaveFace + "/" + System.Math.Ceiling(System.Convert.ToDouble(model.ComicID) / 1000).ToString() + "/" + model.ComicID.ToString() + "." + model.FaceExt);
                            }
                            model.Initial = System.Convert.ToChar(Factory.GetCharSpellCode(ComicName));
                            model.Intro = Intro;
                            model.Language = Language.ToInt();
                            model.OtherName = OtherName;
                            model.State = State.ToInt();
                            model.Tag = Tag;
                            base.Response.Write(((new Yom.Data.Comic().Update(model)) ? "1" : "0"));
                        }
                    }
                    break;
                case "Accept":
                    if (!string.IsNullOrEmpty(ID))
                    {
                        Yom.Model.Comic model = new Yom.Data.Comic().GetModel(ID.ToLong());
                        model.IsCheck = true;
                        base.Response.Write(((new Yom.Data.Comic().Update(model)) ? "1" : "0"));
                    }
                    break;
                case "Canel":
                    if (!string.IsNullOrEmpty(ID))
                    {
                        Yom.Model.Comic model = new Yom.Data.Comic().GetModel(ID.ToLong());
                        model.IsCheck = false;
                        base.Response.Write(((new Yom.Data.Comic().Update(model)) ? "1" : "0"));
                    }
                    break;
                case "Top":
                    if (!string.IsNullOrEmpty(ID))
                    {
                        Yom.Model.Comic model = new Yom.Data.Comic().GetModel(ID.ToLong());
                        model.IsTop = true;
                        base.Response.Write(((new Yom.Data.Comic().Update(model)) ? "1" : "0"));
                    }
                    break;
                case "Down":
                    if (!string.IsNullOrEmpty(ID))
                    {
                        Yom.Model.Comic model = new Yom.Data.Comic().GetModel(ID.ToLong());
                        model.IsTop = false;
                        base.Response.Write(((new Yom.Data.Comic().Update(model)) ? "1" : "0"));
                    }
                    break;
                case "Del":
                    if (!string.IsNullOrEmpty(ID))
                    {
                        List<Yom.Model.Volume> list1 = new Yom.Data.Volume().GetList(ID.ToLong());
                        foreach (Yom.Model.Volume model1 in list1)
                        {
                            List<Yom.Model.Chapter> list2 = new Yom.Data.Chapter().GetList(model1.VolumeID);
                            foreach (Yom.Model.Chapter model2 in list2)
                            {
                                try
                                {
                                    Fso.DeleteFile(model2.ImgPath);
                                }
                                catch (Exception ex) { }
                                new Yom.Data.Chapter().Delete(model2.ChapterID);
                            }
                            new Yom.Data.Volume().Delete(model1.VolumeID);
                        }
                        try
                        {
                            string dirPath = System.IO.Path.Combine(Factory.RootPath, "Files/Cartoon/" + ID);
                            Fso.DeleteDirectory(dirPath);
                        }
                        catch (Exception ex) { }
                        base.Response.Write(((new Yom.Data.Comic().Delete(ID.ToLong())) ? "1" : "0"));
                    }
                    break;
                case "Make":
                    if (!string.IsNullOrEmpty(ID))
                    {
                        try
                        {
                            Yom.Template.MakeHtml.MakeCartoon(ID.ToLong(), true);
                            base.Response.Write("1");
                        }
                        catch (Exception ex)
                        {
                            base.Response.Write(ex.Message.ToString());
                        }
                    }
                    break;
                default:
                    builder = new StringBuilder();
                    string P = Factory.Request("P");
                    int page = (string.IsNullOrEmpty(P) ? 1 : ((P == "0") ? 1 : P.ToInt()));
                    builder.Append("<div class=\"SubMenu\"><a href=\"javascript:$.Run(5)\">漫画管理</a> | <a href=\"javascript:$.Run(5,1)\">添加漫画</a></div>");
                    builder.Append("<div align=\"left\" style=\"padding:5px;\">");
                    builder.Append("<table border=\"0\" cellpadding=\"2\" cellspacing=\"1\" class=\"TablePanel\">");
                    builder.Append("<tr align=\"center\" class=\"TDHead\">");
                    builder.Append("<td nowrap>漫画名称</td><td nowrap>所属分类</td><td nowrap>漫画作者</td><td nowrap>漫画状态</td><td nowrap>最后章节</td><td nowrap>更新时间</td><td nowrap>&nbsp;</td></tr>");
                    List<Yom.Model.Comic> list = new Yom.Data.Comic().GetList(13, page, "", false);
                    int num = 1;
                    foreach (Yom.Model.Comic model in list)
                    {
                        builder.Append("<tr><td align=\"center\"><a href=\"javascript:$.Run(5,2," + model.ComicID.ToString() + "," + page + ")\">" + model.ComicName + "</a></td>");
                        builder.Append("<td align=\"center\">" + new Yom.Data.Category().GetModel(model.CategoryID).CategoryName + "</td>");
                        builder.Append("<td align=\"center\">" + model.Author + "</td>");
                        builder.Append("<td align=\"center\">" + new Yom.Data.Comic().GetState(model.State) + "</td>");
                        builder.Append("<td align=\"center\">" + new Yom.Data.Volume().GetLastVolume(model.ComicID).VolumeName + "</td>");
                        builder.Append("<td align=\"center\">" + model.LastTime.ToString() + "</td>");
                        builder.AppendFormat("<td align=\"center\"><img border=\"0\" src=\"Images/Admin/{1}.png\" width=\"16\" height=\"16\" onclick=\"$.Run(5,{7},{2},{6})\" alt=\"{3}\" style=\"cursor:pointer\" />&nbsp;<img border=\"0\" src=\"Images/Admin/{4}.png\" width=\"16\" height=\"16\" onclick=\"$.Run(5,{8},{2},{6})\" alt=\"{5}\" style=\"cursor:pointer\" />&nbsp;<img border=\"0\" src=\"Images/Admin/make.png\" alt=\"生成漫画HTML\" width=\"16\" height=\"16\" onclick=\"$.Run(5,8,{6},{2})\" style=\"cursor:pointer\" />&nbsp;<img border=\"0\" src=\"Images/Admin/folder.png\" width=\"16\" height=\"16\" onclick=\"$.Run(6,0,0,{2})\" alt=\"章节管理\" style=\"cursor:pointer\" />&nbsp;<img border=\"0\" src=\"Images/Admin/del.gif\" width=\"16\" height=\"16\" alt=\"删除漫画\" onclick=\"$.Run(5,3,{2},{6})\" style=\"cursor:pointer\" /></td>", num, ((model.IsCheck) ? "canel" : "accept"), model.ComicID, ((model.IsCheck) ? "取消审核" : "通过审核"), ((model.IsTop) ? "down" : "top"), ((model.IsTop) ? "取消推荐" : "推荐本漫画"), page, ((!model.IsCheck) ? 4 : 5), ((!model.IsTop) ? 6 : 7));
                        builder.Append("</tr>");
                    }
                    builder.Append("</table><br /><br /><div id=\"ShowMsg\" style=\"color:red\"></div>");
                    builder.Append("<div class=\"more\">" + Factory.GetPager(13, page, new Yom.Data.Comic().GetCount(), "javascript:$.Run(5,0,<{Page}>)") + "</div>");
                    base.Response.Write(builder.ToString());
                    break;
            }
        }
    }
}